import { 获取商品分类 } from '@/api_common/shop'
import { B端店铺获取自身店铺信息 } from '@/api_common/store'
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useShopPinia = defineStore(
  '商城持久化',
  () => {
    const categoryTreeData = ref({})
    const 商品一级分类 = computed(() => {
      return categoryTreeData.value.filter(category => category.parentId === 0)
    })

    const 入驻信息 = ref({})

    async function 获取商品分类api() {
      const { data, code } = await 获取商品分类()
      if (code === 0) {
        categoryTreeData.value = buildTreeData(data)
      }
    }

    async function B端店铺获取自身店铺信息Api() {
      const { data, code } = await B端店铺获取自身店铺信息()
      if (code === 0) {
        入驻信息.value = data
      }
    }

    const 获取商家签约分类 = computed(() => {
      if (入驻信息.value && 入驻信息.value.businessScope) {
        return 商品一级分类.value.filter(category =>
          入驻信息.value.businessScope.includes(category.ID),
        )
      } else {
        return []
      }
    })

    const 商家添加商品信息 = ref({})
    return {
      categoryTreeData,
      获取商品分类api,
      商品一级分类,
      入驻信息,
      B端店铺获取自身店铺信息Api,
      获取商家签约分类,
      商家添加商品信息,
    }
  },
  {
    persist: {
      // persist: true,
      exclude:['商家添加商品信息']
    },
  },
)
